<?php
	session_start();
	include("../Data/User.php");
	function changeURL($path)
	{
		echo '<script type = "text/javascript">';
		echo 'location = "'.$path.'";';
		echo '</script>';
	}
	include("../lib/DataProvider.php");
	$thanhcong=false;
	if(isset($_POST['btnDangKy']))
	{
		$loai="B";
		$kichhoat=1;
		if($_POST['rdbLoai']=="rdbSeller")
		{
			$loai="S";
			$kichhoat=0;
		}		
		$us=$_POST['txtTenDangNhap'];
		$pw=$_POST['txtMatKhau'];
		$email=$_POST['txtEmail'];
		$hoten=$_POST['txtHoTen'];
		$ngay="";
		if(isset($_POST['cmbNgay']))
			$ngay=$_POST['cmbNgay'];
		$thang="";
		if(isset($_POST['cmbThang']))
			$thang=$_POST['cmbThang'];
		$nam="";
		if(isset($_POST['cmbNam']))
			$nam=$_POST['cmbNam'];
		$gioitinh="Nam";
		if($_POST['rdbGioitinh']="rdbNu")
			$gioitinh="Nữ";
		$dienthoai="";
		if(isset($_POST['txtDienThoai']))
			$dienthoai=$_POST['txtDienThoai'];
		$cmnd="";
		if(isset($_POST['txtCMND']))
			$cmnd=$_POST['txtCMND'];
		$tinh="";
		if(isset($_POST['cmbTinh']))
			$tinh=$_POST['cmbTinh'];
		$huyen="";
		if(isset($_POST['cmbHuyen']))
			$huyen=$_POST['cmbHuyen'];
		$xa="";
		if(isset($_POST['txtXa']))
			$xa=$_POST['txtXa'];
		$ap="";
		if(isset($_POST['txtAp']))
			$ap=$_POST['txtAp'];
		$tenduong="";
		if(isset($_POST['txtTenDuong']))
			$tenduong=$_POST['txtTenDuong'];
		$sonha="";
		if(isset($_POST['txtSoNha']))
			$sonha=$_POST['txtSoNha'];
		$avatar="";
		$loiupload=false;
		if($_FILES['Anh']['size']>0 && $_FILES['Anh']['size']<3145729)		
		{
			if ((($_FILES["Anh"]["type"] == "image/gif")
			|| ($_FILES["Anh"]["type"] == "image/jpeg")
			|| ($_FILES["Anh"]["type"] == "image/png")
			|| ($_FILES["Anh"]["type"] == "image/pjpeg"))
			)
			{
				$filename=$_FILES['Anh']['name'];
				$tmpname=$_FILES['Anh']['tmp_name'];
				$filesize=$_FILES['Anh']['size'];
				$filetype=$_FILES['Anh']['type'];
				if (file_exists("../images/" . $tmpname))
				{
					echo $_FILES["Anh"]["name"] . " đã tồn tại. ";
					$loiupload=true;
				}
				else
				{
					move_uploaded_file($_FILES["Anh"]["tmp_name"],
					"../img/users/" . $_FILES["Anh"]["name"]);
					$avatar="img/users/" . $_FILES["Anh"]["name"];				
				}
			}else {
			echo "Lỗi! không thể upload file.";
			$loiupload=true;
			}
		}
		else{			
			echo "Lỗi! bạn vui lòng kiểm tra lại dung lượng file muốn upload(0 Mb <size< 3 Mb)";
			$loiupload=true;
		}
		//tạo mã người dùng
		date_default_timezone_set('Asia/Saigon');		
	 	$now = getdate();
		$head=  $loai.($now["year"] % 100).date("m",$now["mon"]);
			//tạo số thứ tự
		$soluong=DataProvider::ExecuteQuery("select * from nguoidung where MAND like '".$head."'");
		$stt=mysql_num_rows($soluong)+1;
		$stt=sprintf("%04s", $stt);
		$flag=true;
		$MaND=$head.$stt;
		while($flag==true)
		{
			$ds=DataProvider::ExecuteQuery("select * from nguoidung where MAND = '".$MaND."'");
			if(mysql_num_rows($ds)>0)
			{
				$stt=$stt+1;
				$stt=sprintf("%04s", $stt);
				$MaND=$head.$stt;
				$flag=true;
			}else $flag=false;
		}		
		$sql1="insert into nguoidung(MAND,TENDANGNHAP,MATKHAU,MALND,EMAIL,KICHHOAT,BIXOA) values('".$MaND."','".$us."','".$pw."','".$loai."','".$email."',".$kichhoat.",0)";
		$result=DataProvider::ExecuteQuery($sql1);
		echo "<br />";
		$sql2="insert into hoso(MAHS,HOTEN,NGAYSINH,THANGSINH,NAMSINH,GIOITINH,SDT,CMND,AVATAR) values('".$MaND."','".$hoten."',".$ngay.",".$thang.",".$nam.",'".$gioitinh."','".$dienthoai."','".$cmnd."','".$avatar."')";
		$result=DataProvider::ExecuteQuery($sql2);
		echo "<br />";
		$sql3="insert into diachi(MADIACHI,MATINH,MAHUYEN,XA,AP,TENDUONG,SONHA) values('".$MaND."','".$tinh."','".$huyen."','".$xa."','".$ap."','".$tenduong."','".$sonha."')";
		$result=DataProvider::ExecuteQuery($sql3);
		$thanhcong=true;
	}else{
		changeURL("../index.php");
	}
?>
<?php
	if($thanhcong==true)
	{
		$user = new User;
		$user->setMaND($MaND);
		$user->setTenDangNhap($us);
		$user->setMaLND($pw);
		$user->setEmail($email);
		if($loai=="B")
			$user->setRole("Buyer"); 
		if($loai=="S")
			$user->setRole("Seller"); 
		$user->setBiXoa("0");
		$_SESSION['user'] = serialize($user);
		
		echo '<script type="text/javascript">';
		echo 'alert("Chúc mừng! Bạn đã tạo thành công tài khoảng cho riêng mình");';
		echo '</script>';
		changeURL("../index.php");
	}
?>